//
//  ChatTargetView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/11/5.
//

import SwiftUI

//引用试图
struct ChatTargetView2: View {
    var bean: ChatBean
    var width: CGFloat
    let dismiss: () -> Void
    var body: some View {
        let picture = bean.getTargetPicture()
        HStack {
            Text(bean.getTargetChatStr())
                .font(.system(size: 12))
                .foregroundStyle(.bgNormalText4)
                .lineLimit(2)
            Spacer()
            if let picture = picture {
                pictureView(picture: picture).padding(.leading, 30)
            }
            Image(.appIconClose3)
                .resizable()
                .frame(width: 15, height: 15)
                .contentShape(Rectangle())
                .onTapGesture {
                    dismiss()
                }
               
        }.padding(.leading, 10)
            .padding(.trailing, 5)
            .padding(.vertical, 5)
            .frame(width: width)
            .background(Color.hex("#8A8A8A").opacity(0.2))
            .cornerRadius(5)
    }
}

extension ChatTargetView2 {
    func pictureView(picture: EmojiPictureBean) -> some View {
        let itemWidth: CGFloat = 40
        let itemHeight: CGFloat = 40
        return VStack {
            if let mediaItem = EmojiPictureUtils.shared.getMediaItem(picture) {
                PictureItemView(item: mediaItem, itemWidth: itemWidth, itemHeight: itemHeight, cornerRadius: 5)
            } else {
                Image(.appDefaultLogo)
                    .resizable()
                    .scaledToFill()
                    .padding(.horizontal, itemWidth / 4)
                    .padding(.vertical, itemWidth / 4)
            }
        }.frame(width: itemWidth, height: itemHeight)
            .clipShape(RoundedRectangle(cornerRadius: 5))
    }
}
#Preview {
    ChatTargetView2(bean: ChatBean(), width: 300) {}
}
